package com.crm.system.service.impl.customerManager;

import com.crm.common.model.DhPageInfo;
import com.crm.model.entity.customerManager.CustomerContactPerson;
import com.crm.model.entity.customerManager.Exchange;
import com.crm.model.entity.customerManager.ExchangeProduct;
import com.crm.service.customerManager.CustomerContactPersonDbService;
import com.crm.service.customerManager.CustomerExchangeDbService;
import com.crm.service.customerManager.CustomerExchangeProductDbService;
import com.crm.system.service.customerManager.CustomerExchangeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ Author ：baijian
 * @ Date   ：2021/5/7 11:08
 */
@Service
public class CustomerExchangeServiceImpl implements CustomerExchangeService {
    @Autowired
    CustomerExchangeDbService customerExchangeDbService;

    @Autowired
    CustomerExchangeProductDbService customerExchangeProductDbService;

    /**
     * 客户详情-兑换列表
     * @param pageIndex 页码
     * @param pageSize 条数
     * @param sortName 分组名称
     * @param sortOrder 分组方式
     * @param customerId 客户id
     * @return
     */
    @Override
    public DhPageInfo<Exchange> exchangeList(Integer pageIndex, Integer pageSize, String sortName, String sortOrder, String customerId) {
        if (pageIndex != null && pageSize != null) {
            PageHelper.startPage(pageIndex, pageSize);
        }

        String order = "create_tm desc";

        List<Exchange> list = customerExchangeDbService.exchangeList(order, customerId);
        PageInfo<Exchange> pageInfo = new PageInfo<Exchange>(list);
        return new DhPageInfo<Exchange>(pageInfo, list);
    }

    /**
     * 客户详情-兑换明细
     * @param exchangeId 兑换id
     * @return
     */
    @Override
    public Map<String, Object> exchangeDetail(String exchangeId) {
        Map<String, Object> map = new HashMap<>();

        Exchange exchange = customerExchangeDbService.getExchangeById(exchangeId);

        //产品兑换状态  兑换前
        List<ExchangeProduct> beforeExchangelist = customerExchangeProductDbService.exchangeProductList(exchangeId,1);

        //产品兑换状态  兑换后
        List<ExchangeProduct> afterExchangelist = customerExchangeProductDbService.exchangeProductList(exchangeId,2);

        map.put("detail",exchange);
        map.put("beforeExchange",beforeExchangelist);
        map.put("afterExchange",afterExchangelist);

        return map;
    }
}
